
import com.jme3.math.Vector3f;
import com.jme3.network.Client;
import com.jme3.network.Message;
import com.jme3.network.MessageListener;

/**
 *
 * @author David Zhang
 */
public class ChatHandler implements MessageListener<Client> {
        public void messageReceived(Client source, Message m) {
            ChatMessage chat = (ChatMessage) m;
            if(chat.getName().equals("chat")){
                Main.app.gui.chatAdd(chat.getMessage());
            }
            else if(chat.getName().equals("join")){
                if(Main.app.client.myIndex == -1){
                    Main.app.client.myIndex = Integer.parseInt(chat.getMessage());
                    for(int i = 0; i < Main.app.client.myIndex + 1; i++){
                        Main.app.client.players.add(new Player(Main.app.getAssetManager().loadModel("Models/Oto/Oto.mesh.xml"), Main.app.playerNode));
                        Main.app.client.players.get(i).add(Main.app.bas);
                        if(i == Main.app.client.myIndex) Main.app.playerNode.detachChild(Main.app.client.players.get(Main.app.client.myIndex).getModel());
                    }
                    Main.app.client.playernum = Main.app.client.myIndex + 1;
                    System.out.println(Main.app.client.myIndex);
                }
                else{
                    Main.app.client.players.add(new Player(Main.app.getAssetManager().loadModel("Models/Oto/Oto.mesh.xml"), Main.app.playerNode));
                    Main.app.client.players.get(Main.app.client.players.size() - 1).add(Main.app.bas);
                    Main.app.client.playernum++;
                }
            }
            else if(chat.getName().equals("leave")){
                if(Integer.parseInt(chat.getMessage()) < Main.app.client.myIndex){
                    Main.app.client.players.remove(Integer.parseInt(chat.getMessage()) - 1);
                    Main.app.client.myIndex--;
                }
                Main.app.client.playernum--;
            }
            else if(chat.getName().equals("coordinates")){
                String chatmessage = chat.getMessage();
                int tempindex = Integer.parseInt(chatmessage.substring(0, chatmessage.indexOf(":")));
                float x = Float.parseFloat(chatmessage.substring(chatmessage.indexOf('x') + 1, chatmessage.indexOf('y')));
                float y = Float.parseFloat(chatmessage.substring(chatmessage.indexOf('y') + 1, chatmessage.indexOf('z')));
                float z = Float.parseFloat(chatmessage.substring(chatmessage.indexOf('z') + 1, chatmessage.length()));
                try{
                    try{
                        Main.app.client.players.get(tempindex).getControl().setPhysicsLocation(new Vector3f(x, y, z));
                    } catch (IndexOutOfBoundsException d){}
                } catch (ArrayIndexOutOfBoundsException e){}
            }
            else if(chat.getName().equals("direction")){
                String chatmessage = chat.getMessage();
                int tempindex = Integer.parseInt(chatmessage.substring(0, chatmessage.indexOf(":")));
                float x = Float.parseFloat(chatmessage.substring(chatmessage.indexOf('x') + 1, chatmessage.indexOf('y')));
                float y = Float.parseFloat(chatmessage.substring(chatmessage.indexOf('y') + 1, chatmessage.indexOf('z')));
                float z = Float.parseFloat(chatmessage.substring(chatmessage.indexOf('z') + 1, chatmessage.length()));
                try{
                    try{
                        Main.app.client.players.get(tempindex).getControl().setViewDirection(new Vector3f(x, y, z));
                    } catch (IndexOutOfBoundsException d){}
                } catch (ArrayIndexOutOfBoundsException e){}
            }
        }
}